// 校验手机号格式是否正确（只检测大陆手机号）
export const checkPhoneValid = (number) => {
  let tempNumber = number.replace(/[^\d]/g, "");
  const reg = /^(\+?0?86\-?)?1[3456789]\d{9}$/;
  return reg.test(tempNumber);
};

// 校验手机号格式是否正确（兼容国内国际号码）
export const checkGlobalPhoneValid = (number) => {
  // 兼容国内/国际手机号
  // 参见 https://cloud.tencent.com/developer/article/1140913

  const phones = {
    "ar-DZ": /^(\+?213|0)(5|6|7)\d{8}$/,
    "ar-SY": /^(!?(\+?963)|0)?9\d{8}$/,
    "ar-SA": /^(!?(\+?966)|0)?5\d{8}$/,
    "en-US": /^(\+?1)?[2-9]\d{2}[2-9](?!11)\d{6}$/,
    "cs-CZ": /^(\+?420)? ?[1-9][0-9]{2} ?[0-9]{3} ?[0-9]{3}$/,
    "de-DE": /^(\+?49[ \.\-])?([\(]{1}[0-9]{1,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
    "da-DK": /^(\+?45)?(\d{8})$/,
    "el-GR": /^(\+?30)?(69\d{8})$/,
    "en-AU": /^(\+?61|0)4\d{8}$/,
    "en-GB": /^(\+?44|0)7\d{9}$/,
    "en-HK": /^(\+?852\-?)?[569]\d{3}\-?\d{4}$/,
    "en-IN": /^(\+?91|0)?[789]\d{9}$/,
    "en-NZ": /^(\+?64|0)2\d{7,9}$/,
    "en-ZA": /^(\+?27|0)\d{9}$/,
    "en-ZM": /^(\+?26)?09[567]\d{7}$/,
    "es-ES": /^(\+?34)?(6\d{1}|7[1234])\d{7}$/,
    "fi-FI": /^(\+?358|0)\s?(4(0|1|2|4|5)?|50)\s?(\d\s?){4,8}\d$/,
    "fr-FR": /^(\+?33|0)[67]\d{8}$/,
    "he-IL": /^(\+972|0)([23489]|5[0248]|77)[1-9]\d{6}/,
    "hu-HU": /^(\+?36)(20|30|70)\d{7}$/,
    "it-IT": /^(\+?39)?\s?3\d{2} ?\d{6,7}$/,
    "ja-JP": /^(\+?81|0)\d{1,4}[ \-]?\d{1,4}[ \-]?\d{4}$/,
    "ms-MY": /^(\+?6?01){1}(([145]{1}(\-|\s)?\d{7,8})|([236789]{1}(\s|\-)?\d{7}))$/,
    "nb-NO": /^(\+?47)?[49]\d{7}$/,
    "nl-BE": /^(\+?32|0)4?\d{8}$/,
    "nn-NO": /^(\+?47)?[49]\d{7}$/,
    "pl-PL": /^(\+?48)? ?[5-8]\d ?\d{3} ?\d{2} ?\d{2}$/,
    "pt-BR": /^(\+?55|0)\-?[1-9]{2}\-?[2-9]{1}\d{3,4}\-?\d{4}$/,
    "pt-PT": /^(\+?351)?9[1236]\d{7}$/,
    "ru-RU": /^(\+?7|8)?9\d{9}$/,
    "sr-RS": /^(\+3816|06)[- \d]{5,9}$/,
    "tr-TR": /^(\+?90|0)?5\d{9}$/,
    "vi-VN": /^(\+?84|0)?((1(2([0-9])|6([2-9])|88|99))|(9((?!5)[0-9])))([0-9]{7})$/,
    "zh-CN": /^(\+?0?86\-?)?1[3456789]\d{9}$/,
    "zh-TW": /^(\+?886\-?|0)?9\d{8}$/,
  };

  const regArr = Object.values(phones);

  return regArr.some((reg) => reg.test(number));
};

// 校验邮箱格式是否正确
export const checkEmailValid = (email) => {
  const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  return reg.test(email);
};

// 校验座机格式是否正确
export const checkTelValid = (tel) => {
  const reg = /^(\d{3,4}\-)?(\d{7,8})+(\-\d{1,6})?$/;
  return reg.test(tel);
};
